package com.shen.eduservice.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shen.commonutils.Result;
import com.shen.eduservice.entity.EduTeacher;
import com.shen.eduservice.entity.vo.TeacherQuery;
import com.shen.eduservice.service.EduTeacherService;
import com.shen.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author shenkaituo
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    EduTeacherService eduTeacherService;
    @GetMapping("findAll")
    public Result findAllTeacher(){

        List<EduTeacher> teachers=new ArrayList<>();
        teachers=eduTeacherService.list(null);
        return Result.ok().data("items",teachers);
        //Result.ok() 已经设置了处了result的属性 并且返回了result对象 在.data()添加参数
    }
    @DeleteMapping("{id}")
    public Result deleteTeacher(@PathVariable String id){
        boolean flag = eduTeacherService.removeById(id);
        if(flag){
            return Result.ok();
        }
        else {
            return Result.error();
        }

    }
    @GetMapping("pageTeacher/{current}/{limit}")
    //current 当前页 limit 一页显示记录的个数
    public Result pageTeacher(@PathVariable("current") long current,
                              @PathVariable("limit") long limit){
        //创建page对象
        Page<EduTeacher> page=new Page<>(current,limit);
        eduTeacherService.page(page,null);//wrapper 条件 会把结果放到page里面
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();//每页 数据list集合
//        Map map=new HashMap();
//        map.put("total",total);
//        map.put("items",records);
//        return  Result.ok().data(map);
        return Result.ok().data("total",total).data("rows",records);//两种方式都行
    }
    @PostMapping ("pageTeacherCondition/{current}/{limit}")
    //current 当前页 limit 一页显示记录的个数
    public Result pageTeacherCondition(@PathVariable("current") long current,
                                       @PathVariable("limit") long limit,
                                       @RequestBody(required = false)   TeacherQuery teacherQuery){
        //System.out.println(teacherQuery);
        //创建page对象
        Page<EduTeacher> page=new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> teacherQueryWrapper=new QueryWrapper<>();
        System.out.println(teacherQuery);
        String  name=teacherQuery.getName(); System.out.println(name);
        Integer level=teacherQuery.getLevel(); System.out.println(level);
        String begin = teacherQuery.getBegin(); System.out.println(begin);
        String end = teacherQuery.getEnd(); System.out.println(end);
        System.out.println("/////");
        if(!StringUtils.isEmpty(name)){
            teacherQueryWrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            teacherQueryWrapper.eq("level",level);

        }
        if(!StringUtils.isEmpty(begin)){
            teacherQueryWrapper.ge("gmt_create",begin);//要求是表中的名字
        }
        if(!StringUtils.isEmpty(end)){
            teacherQueryWrapper.le("gmt_modified",end);
        }
        teacherQueryWrapper.orderByDesc("gmt_create");
        eduTeacherService.page(page,teacherQueryWrapper);//wrapper 条件 会把结果放到page里面
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();

        return Result.ok().data("total",total).data("rows",records);//两种方式都行
    }
    // 添加讲师
    @PostMapping("addTeacher")
    //requestbody 把jsion对象封装到对象 post请求 required =flase 表示参数值可以没有 为空
    public Result addTeacher(@RequestBody EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){return  Result.ok();}
        else {return  Result.error();}
    }
    //根据id得到讲师

    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable String id){

        try{
            //int i=10/0;
        }
        catch (Exception e){
            throw new GuliException(2001,"执行了自定义异常处理");
        }

        EduTeacher eduTeacher = eduTeacherService.getById(id);
        //System.out.println(eduTeacher);
        System.out.println(id);
        System.out.println(eduTeacher);
        return Result.ok().data("teacher",eduTeacher);

        //return Result.ok().data("teacher",eduTeacher);
    }
    //修改讲师
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if(flag){ return Result.ok();}
        else  return Result.error();

    }

}

